Data: https://www.kaggle.com/datasets/szrlee/stock-time-series-20050101-to-20171231?resource=download
stocks = read.csv("Data/all_stocks_2006-01-01_to_2018-01-01.csv")
stocks = stocks %>%
mutate(Date = as.Date(Date)) %>%
filter(Name == c("HD", "AAPL", "MSFT", "WMT", "MCD")) %>%
filter(Date >= "2015-01-01")
## Warning in Name == c("HD", "AAPL", "MSFT", "WMT", "MCD"): longer object length
## is not a multiple of shorter object length
head(stocks)
## Date Open High Low Close Volume Name
## 1 2015-01-06 106.54 107.43 104.63 106.26 65797116 AAPL
## 2 2015-01-13 111.43 112.80 108.91 110.22 67091928 AAPL
## 3 2015-01-21 108.95 111.06 108.27 109.55 48575897 AAPL
## 4 2015-01-28 117.62 118.12 115.31 115.31 146477063 AAPL
## 5 2015-02-04 118.50 120.51 118.31 119.56 70149743 AAPL
## 6 2015-02-11 122.77 124.92 122.50 124.88 73561797 AAPL
stocks_plot = ggplot(data = stocks, aes(x=Date, y = Close, color = Name)) +
geom_line() +
theme_minimal() +
scale_x_date(breaks = "1 year",
minor_breaks = "1 month",
date_labels = "%Y",
limits=c(as.Date("2015-01-01"), NA)) +
ylab("Closing Price") + xlab("") +
ggtitle("Stock Information for Several Large Companies") +
scale_color_viridis(discrete = TRUE) +
guides(color=guide_legend(title="Ticker"))
volume_plot = ggplot(data = stocks, aes(x=Date, y = Volume/1000000, color = Name)) +
geom_line() +
theme_minimal() +
scale_x_date(breaks = "1 year",
minor_breaks = "1 month",
date_labels = "%Y",
limits=c(as.Date("2015-01-01"), NA)) +
ylab("Volume (in millions)") + xlab("") +
scale_color_viridis(discrete = TRUE) +
guides(color=guide_legend(title="Ticker"))
stocks_plotly = ggplotly(stocks_plot)
volume_plotly = ggplotly(volume_plot)
stocks_plotly
volume_plotly
stocks_plty_facet <- subplot(list(stocks_plotly,volume_plotly),
nrows = 2,
shareX = TRUE,
titleX = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x")
stocks_plty_facet
stocks2 = stocks %>%
filter(Name == "AAPL")
stocks_plty2 = plot_ly(stocks2, x = ~Date) %>%
add_lines(y = ~Close, name= "Closing Price")
volume_plty2 = plot_ly(stocks2, x = ~Date) %>%
add_lines(y = ~Volume, name = "Volume")
plty_facet = subplot(list(stocks_plty2, volume_plty2),
nrows = 2, shareX = TRUE, titleX = FALSE) %>%
rangeslider() %>%
layout(hovermode = "x") %>%
layout(
title="Apple",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(step = "all"))),
rangeslider = list(type = "date")))
plty_facet